import { defineStore } from 'pinia';
import { ref, onMounted } from 'vue';
import request from '@/utils/request';
import { showNotification } from '@/utils/notify';

// 定义角色类型
type Role = {
    roleName: string;
    roleCode: string;
    description: string;
};

// 定义角色分页数据类型
type RolePage = {
    code: number,
    message: string,
    data: {
        records: Role[],
        total: number,
        size: number,
        current: number,
        orders: [],
        optimizeCountSql: boolean,
        hitCount: boolean,
        "countId": null,
        "maxLimit": null,
        searchCount: boolean,
        pages: number
    }
};

export const useRoleStore = defineStore('role', () => {
    const roles = ref<Role[]>([]);
    const total = ref(0); // 总角色数量
    const currentPage = ref(1); // 当前页码
    const pageSize = ref(10); // 每页显示的角色数量

    const fetchRoles = async () => {
        try {
            const { data } = await request.get<RolePage>(
                `/api/roles/page?current=${currentPage.value}&size=${pageSize.value}`
            );

            roles.value = data.records;
            total.value = data.total;
        } catch (error) {
            showNotification("获取角色列表失败", true);
        }
    };

    const addRole = async (role: Role) => {
        const { code, data } = await request.post<{ code: number; data: object }>('/api/roles/add', role);
        if (code === 200) {
            showNotification("角色添加成功", false);
            fetchRoles()

        } else {
            showNotification("角色添加失败", true);
        }
    };

    const updateRole = async (index: number, updatedRole: Role) => {
        try {
            const roleCode = updatedRole.roleCode;
            const { code, data } = await request.post<{ code: number; data: object }>(
                `/api/roles/update/${roleCode}`,
                updatedRole
            );
            if (code === 200) {
                showNotification("角色更新成功", false);
                fetchRoles()
            } else {
                showNotification("角色更新失败", true);
            }
        } catch (error) {
            showNotification("角色更新失败", true);
        }
    };

    const deleteRole = async (index: number) => {
        try {
            const roleCode = roles.value[index].roleCode;
            const { code, data } = await request.post<{ code: number; data: object }>(
                `/api/roles/remove/${roleCode}`
            );
            if (code === 200) {
                roles.value.splice(index, 1);
                showNotification("角色删除成功", false);
            } else {
                showNotification("角色删除失败", true);
            }
        } catch (error) {
            showNotification("角色删除失败", true);
        }
    };

    const changePage = (page: number) => {
        currentPage.value = page;
        fetchRoles();
    };

    onMounted(() => {
        fetchRoles();
    });

    return {
        roles,
        total,
        currentPage,
        pageSize,
        addRole,
        updateRole,
        deleteRole,
        fetchRoles,
        changePage
    };
});